package app

import (
	"chatgpt/model/base"
	"github.com/slclub/go-tips"
)

// benefit types 充值类型对应
const (
	BENEFIT_VIP         = "vip"
	BENEFIT_CASH        = "cash"
	BENEFIT_FREE_EXPIRE = "free_expire"
)

// benefit times
const (
	EXPIRE_DAY = 24 * 3600
)

// profit 系统权限 收益等
const ()

type Benefit struct {
	BenefitID int      `gorm:"primaryKey;column:benefit_id;autoIncrement"`
	Remark    string   `gorm:"size:255;comment:备注说明"`
	Data      string   `gorm:"size:255;comment:备用配置数据"`
	Action    string   `gorm:"size:128;comment:权益类型,相当于权限的角色，例如VIP"`
	Scopes    []*Scope `gorm:"-"` // 权限
	DataAny   any
}

func (self *Benefit) ConvFromMap(m map[string]any) {
	if val, ok := m["BenefitID"]; ok && self.BenefitID == 0 {
		self.BenefitID = tips.Int(val)
	}
	if val, ok := m["Remark"]; ok {
		self.Remark = tips.String(val)
	}
	if val, ok := m["Data"]; ok {
		self.Data = tips.String(val)
	}
	if val, ok := m["Action"]; ok {
		self.Action = tips.String(val)
	}
}

func (self *Benefit) ConvToMap() map[string]any {
	return map[string]any{
		"BenefitID": self.BenefitID,
		"Remark":    self.Remark,
		"Data":      self.Data,
		"Action":    self.Action,
	}
}

// 用户权益表
type BenefitUser struct {
	base.ModelID
	//UUID        string `gorm:"size:128;column:uuid;comment:UUID"`
	base.UUIDR
	BenefitID     int    `gorm:"type:int(10);column:benefit_id; comment:权益ID"`
	ExpireStart   int    `gorm:"type:int(10);comment:受益开始时间"`
	ExpireAt      int    `gorm:"type:int(10);comment:受益过期时间"`
	DeletedAt     int    `gorm:"type:int(10);comment:删除"`
	BenefitInt    int    `gorm:"type:int(10);comment:权益数值"`
	BenefitString string `gorm:"size:255;comment:权益相关数据"`
}

func (self *BenefitUser) TableName() string {
	return "gpt_benefit_user"
}

func (self *BenefitUser) ConvFromMap(m map[string]any) {
	self.ModelID.ConvFromMap(m)
	self.UUIDR.ConvFromMap(m)
	if val, ok := m["BenefitID"]; ok {
		self.BenefitID = tips.Int(val)
	}
	if val, ok := m["ExpireStart"]; ok {
		self.ExpireStart = tips.Int(val)
	}
	if val, ok := m["ExpireAt"]; ok {
		self.ExpireAt = tips.Int(val)
	}
	if val, ok := m["DeletedAt"]; ok {
		self.DeletedAt = tips.Int(val)
	}
	if val, ok := m["BenefitInt"]; ok {
		self.BenefitInt = tips.Int(val)
	}
	if val, ok := m["BenefitString"]; ok {
		self.BenefitString = tips.String(val)
	}
}

func (self *BenefitUser) ConvToMap() map[string]any {
	return map[string]any{
		"ID":            self.ID,
		"UUID":          self.UUID,
		"BenefitID":     self.BenefitID,
		"ExpireStart":   self.ExpireStart,
		"ExpireAt":      self.ExpireAt,
		"DeletedAt":     self.DeletedAt,
		"BenefitInt":    self.BenefitInt,
		"BenefitString": self.BenefitString,
	}
}

// 权益 - 权限 关联表
type BenefitScope struct {
	base.ModelID
	BenefitID int `gorm:"type:int(10);column:benefit_id; comment:权益ID"`
	ScopeID   int `gorm:"type:int(10);column:scope_id;comment:权限ID"`
	Scope     *Scope
}

func (self *BenefitScope) ConvFromMap(m map[string]any) {
	self.ModelID.ConvFromMap(m)
	if val, ok := m["BenefitID"]; ok {
		self.BenefitID = tips.Int(val)
	}
	if val, ok := m["ScopeID"]; ok {
		self.ScopeID = tips.Int(val)
	}
}

func (self *BenefitScope) ConvToMap() map[string]any {
	return map[string]any{
		"ID":        self.ID,
		"BenefitID": self.BenefitID,
		"ScopeID":   self.ScopeID,
	}
}
